<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Users.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_Model_DbTable_Likes extends Engine_Db_Table
{
  protected $_name = 'likes';
  
  protected $_rowClass = 'Like_Model_Like';
  
  public function getLikes(array $params)
  {
  	return $this->getPaginator($params);
  }
  
  public function getSelect(array $params)
  {
    $userTable = Engine_Api::_()->getItemTable('user');
    $name0 = $userTable->info('name');
    
    $name1 = $this->info('name');

    $membershipTable = Engine_Api::_()->getDbTable('membership', 'user');
    $name2 = $membershipTable->info('name');

    $likeClubTable = Engine_Api::_()->getDbTable('clubs', 'like');
    $name3 = $likeClubTable->info('name');
    
  	$select = $this->select()
  		->setIntegrityCheck(false)
      ->from($name1, array())
      ->joinLeft($name0, $name0.'.user_id = '.$name1.'.user_id');
  		
  	if (!empty($params['club_id'])){
  		$select
  			->where($name1.".like_club_id = {$params['club_id']}");
  	}
  	
  	if (!empty($params['keyword'])){
  		$select
  			->where($name0.".username LIKE '%{$params['keyword']}%' OR ".$name0.".displayname LIKE '%{$params['keyword']}%'");
  	}

    if (!empty($params['friend_id'])){
      $select
        ->joinInner($name2, $name2.'.user_id = '.$name0.'.user_id', array())
        ->where($name2.'.resource_id = ?', $params['friend_id'])
        ->where($name2.'.resource_approved = 1')
        ->where($name2.'.user_approved = 1');
    }

    if (!empty($params['limit'])){
  		$select
  			->limit($params['limit']);
  	}

    if (!empty($params['active'])){
  		$select
        ->joinLeft($name3, $name3.'.club_id = '.$name1.'.like_club_id', array())
  			->where($name3.'.active = ?', (int)$params['active']);
  	}

  	return $select;
  }

  public function getLikedCount($params = array())
  {
    $select = $this->select();
    $name = $this->info('name');

    $likeClubTable = Engine_Api::_()->getDbTable('clubs', 'like');
    $name0 = $likeClubTable->info('name');

    $select
      ->setIntegrityCheck(false)
      ->from($name, array('count' => 'COUNT(*)'))
      ->joinLeft($name0, $name0.'.club_id = '.$name.'.like_club_id', array())
      ->where($name0.'.active = 1');

    if (!empty($params['user_id'])){
      $select
        ->where($name.'.user_id = ?', $params['user_id']);  
    }

    if (!empty($params['object'])){
      $select
        ->where($name0.'.object = ?', $params['object']);
    }

    return (int)$this->getAdapter()->fetchOne($select);
  }
  
  public function getPaginator(array $params)
  {
  	$select = $this->getSelect($params);
    
  	$paginator = Zend_Paginator::factory($select);
  	
  	if (!empty($params['ipp'])) {
			$params['ipp'] = (int)$params['ipp'];
			$paginator->setItemCountPerPage($params['ipp']);
		}
		
		if (!empty($params['p'])) {
			$params['p'] = (int)$params['p'];
			$paginator->setCurrentPageNumber($params['p']);
		}
		
		return $paginator;
  }
}